package com.keyidea.jfxui.core.system;

import cn.hutool.core.bean.BeanUtil;
import com.keyidea.jfxui.core.page.TableDataInfo;
import com.keyidea.jfxui.core.system.domain.AjaxResult;
import com.keyidea.jfxui.core.system.sqlite.entity.SysDictData;
import com.keyidea.jfxui.core.system.sqlite.mapper.SysDictDataMapper;
import com.keyidea.jfxui.core.util.Convert;
import com.keyidea.jfxui.core.util.StringUtils;
import feign.Param;
import feign.QueryMap;
import feign.RequestLine;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 */
@Service
@RequiredArgsConstructor
public class SysDictDataService extends BaseService {

    private final SysDictDataMapper sysDictDataMapper;

    public TableDataInfo list(@QueryMap Map<String, Object> query) {
        SysDictData dictData = BeanUtil.toBean(query, SysDictData.class);
        startPage(query);
        List<SysDictData> list = sysDictDataMapper.selectDictDataList(dictData);
        return getDataTable(list);
    }

    public void export(SysDictData dictData) {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    public AjaxResult getInfo(@Param("dictCode") Long dictCode) {
        return success(sysDictDataMapper.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    public AjaxResult dictType(@Param("dictType") String dictType) {
        List<SysDictData> data = selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    public AjaxResult add(SysDictData data) {
        int row = sysDictDataMapper.insertDictData(data);
        return toAjax(row);
    }

    /**
     * 修改保存字典类型
     */
    public AjaxResult edit(SysDictData data) {
        int row = sysDictDataMapper.updateDictData(data);
        return toAjax(row);
    }

    /**
     * 删除字典类型
     */
    public AjaxResult remove(@Param("dictCodes") String dictCodes) {
        Long[] ids = Convert.toLongArray(dictCodes);
        sysDictDataMapper.deleteDictDataByIds(ids);
        return success();
    }

    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        return null;
    }
}
